<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Authorizer;
use app\admin\model\Replyfansimgtext;
use app\admin\model\Replyfanspoolsetimg;
use think\Db;
use think\Request;
use app\admin\model\Replyfanscategory;
use app\admin\model\Replyfansaccount;
use app\admin\model\Replyfanspool;
use app\admin\model\Shieldrule;
use think\facade\Env;

class Replyfans extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function entrylink()
    {
        return $this->fetch('replyfans/entrylink');
    }
    public function getLinkList()
    {
        $key = $this->request->post('keyword');
        $list = Db::table('nx_entry_link')
            ->where('linkname','like','%'.$key.'%')
            ->where('delstatus','1')
            ->select();
        foreach ($list as $k=>$v){
            $domain = Db::table('nx_bind_domain')->where('id',$v['linkname'])->value('domain');
            $list[$k]['linkname'] = $domain;
        }
        return $this->api_resp_success('','',$list);
    }
    public function linkDetail()
    {
        return $this->fetch('replyfans/linkDetail');
    }
    //添加入口链接
    public function addEntryLink()
    {
        $data = Db::table('nx_bind_domain')->where('delstatus',1)->select();
        $this->assign('data',$data);
        return $this->fetch('replyfans/addEntryLink');
    }
    public function doAddEntryLink()
    {
        $parame = $this->request->param();
        $data = [
            'linkname'=>$parame['domain'],
            'entry_link'=>'',          //接口写好补上
            'save_link'=>$parame['savelink'],
            'void_link'=>$parame['voidlink'],
            'shield_city'=>$parame['city'],
            'is_weight'=>$parame['weight'],
            'remark'=>$parame['remark'],
            'circulation'=>$parame['xh'],
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_entry_link')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    //删除入口链接
    public function delEntryLink()
    {
        $ids = $this->request->post('ids');
        $res = Db::name('nx_entry_link')->where('id','in',$ids)->update(['delstatus'=>4]);
        if($res){
            return $this->api_resp_success('','',$res);
        }else{
            return $this->api_resp_error('');
        }

    }
    //获取入口链接
    public function getEntryLink()
    {
        $id = $this->request->param('aid');
        $list = Db::table('nx_bind_domain')->where(['delstatus'=>1,'is_down'=>1])->select();
        $count = Db::table('nx_bind_domain')->where(['delstatus'=>1,'is_down'=>1])->count();
        if($count == 1){
            $num = 0;
        }else{
            $num = mt_rand(0,$count-1);
        }
        $link = $list[$num]['domain'];
        $url = $link.'/entrylink/pool/'.$id.'/domian/'.$list[$num]['id'];
        $res = Replyfanspool::where('id',$id)->update(['entrylink'=>$url]);
        if($res){
            return $this->api_resp_success('','',$url);
        }else{
            return $this->api_resp_error('获取失败');
        }
    }
    //复粉账号池
    public function replyfanspool()
    {
        return $this->fetch('replyfans/replyfanspool');
    }
    //获取账号池列表
    public function getPoolList()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Replyfanspool::where('delstatus',1)
            ->where('poolname','like','%'.$key.'%')
            ->page($page,$size)->order('createtime desc')->select();
        foreach ($list as $k=>$v){
            $sname = Replyfanscategory::where('id',$v['category_sum'])->value('categoryname');
            $cname = Replyfanscategory::where('id',$v['category_child'])->value('categoryname');
            if($sname == ''){
                $list[$k]['sumname'] = '';
            }else{
                $list[$k]['sumname'] = $sname;
            }
            if($cname == ''){
                $list[$k]['childname'] = '';
            }else{
                $list[$k]['childname'] = $cname;
            }
            $rule = Shieldrule::where('id',$v['shieldrule'])->value('rulename');
            $list[$k]['rulename'] = $rule;
        }
        $pagecount = Replyfanspool::where('delstatus',1)
            ->where('poolname','like','%'.$key.'%')
            ->count();
        return $this->api_resp_success('',array("count"=>$pagecount),$list);
    }
    //添加账号池
    public function addAccountPool()
    {
        //获取屏蔽规则
        $shieldrule = Shieldrule::where('delstatus',1)->select();
        //获取总分类
        $category_sum = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        $this->assign('category_sum',$category_sum);
        $this->assign('shieldrule',$shieldrule);
        return $this->fetch('replyfans/addAccountPool');
    }
    public function doAddPool()
    {
        $data = $this->request->param();
        $udata = [
            'poolname'       => $data['poolname'],
            'category_sum'   => $data['category_sum'],
            'category_child' => $data['category_child'],
            'remark'         => $data['remark'],
            'outerlink'      => $data['outerlink'],
            'is_weight'      => $data['is_weight'],
            'shieldrule'     => $data['shieldrule'],
            'rotarygroups'   => $data['rotarygroups'],
            'imgtext_num'    => $data['imgtext_num'],
            'createtime'     => date('Y-m-d H:i:s',time())
        ];
        $res = Replyfanspool::insertGetId($udata);
        if($res){
            $setimg = [
                ['poolid' => $res, 'level'=> 1 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 2 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 3 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 4 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 5 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 6 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 7 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $res, 'level'=> 8 ,'createtime' => date('Y-m-d H:i:s',time())],
            ];
            Replyfanspoolsetimg::insertAll($setimg);
            return $this->api_resp_success('添加成功','',$res);
        }else{
            return $this->api_resp_error('添加失败');
        }
    }
    //图文设置
    public function setImgText()
    {
        $id  = $this->request->param('aid');
        $this->assign('id',$id);
        return $this->fetch('replyfans/setImgText');
    }
    //获取图文条数分类
    public function getImgTextNum()
    {
        $id  = $this->request->post('mid');
        $num = Replyfanspool::where('id',$id)->value('imgtext_num');
        $imgtextinfo = [];
        for($i=1; $i<=$num; $i++){
            $imgtextid = Replyfanspoolsetimg::where(['poolid'=>$id,'level'=>$i])->value('imgtextid');
            $img = Replyfansimgtext::where(['accountpool'=>$id,'delstatus'=>1,'img_local'=>$i])->select();
            $html  = '<div class="form-group col-md-7 pull-right">';
            $html .= '<label>第'.$i.'条</label>';
            $html .= '<select i_num="'.$i.'" class="form-control tuwen">';
            $html .= '<option value="0">选择图文（不选择默认随机）</option>';
            foreach ($img as $k=>$v){
                if($v['id'] == $imgtextid){
                    $html .= '<option value="'.$v['id'].'" selected="selected" >'.$v['title'].'</option>';
                }else{
                    $html .= '<option value="'.$v['id'].'">'.$v['title'].'</option>';
                }
            }
            $html .= '</select>';
            $html .= '</div>';
            $list[$i] = $html;
            $j = $i-1;
            if($imgtextid == 0){
                $imgtextinfo[$j]['title']  = '';
                $imgtextinfo[$j]['imgurl'] = '';
            }else{
                $imgtextinfo[$j] = Replyfansimgtext::where('id',$imgtextid)->field('title,imgurl')->find();
            }
        }
        return $this->api_resp_success('',array('count'=>$num,'imginfo'=>$imgtextinfo),$list);
    }
    //获取设置的图文信息
    public function getSetImgtextInfo()
    {
        $id = $this->request->post('pid');
        if($id == 0){
            $einfo = [
                'imgurl' => '',
                'title'  => '',
                'id'     => 0
            ];
            return $this->api_resp_success('','',$einfo);
        }
        $info = Replyfansimgtext::where('id',$id)->find();
        if($info){
            return $this->api_resp_success('','',$info);
        }else{
            return $this->api_resp_error('');
        }
    }
    public function doSetImgText()
    {
        $id   = $this->request->post('id');
        $tid   = $this->request->post('tid');
        $tnum = $this->request->post('tnum');
        $exist = Replyfanspoolsetimg::where(['delstatus'=>1,'poolid'=>$id])->count();
        if($exist <= 0 || $exist < $tnum){
            Replyfanspoolsetimg::where(['delstatus'=>1,'poolid'=>$id])->delete();
            $adata = [
                ['poolid' => $id, 'level'=> 1 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 2 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 3 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 4 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 5 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 6 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 7 ,'createtime' => date('Y-m-d H:i:s',time())],
                ['poolid' => $id, 'level'=> 8 ,'createtime' => date('Y-m-d H:i:s',time())],
            ];
            Replyfanspoolsetimg::insertAll($adata);
        }
        $udata = [
            'imgtextid'  => $tid,
            'updatetime' => date('Y-m-d H:i:s',time())
        ];
        $res = Replyfanspoolsetimg::where(['level'=>$tnum,'poolid'=>$id])->update($udata);
        if($res){
            return $this->api_resp_success('','','');
        }else{
            return $this->api_resp_error('');
        }
    }
    //编辑账号池
    public function editaccountpool()
    {
        $id = $this->request->param('aid');
        $data = Replyfanspool::where('id',$id)->find();
        //获取屏蔽规则
        $shieldrule = Shieldrule::where('delstatus',1)->select();
        //获取总分类
        $category_sum = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        $category_child = Replyfanscategory::where(['delstatus'=>1,'level'=>2,'pid'=>$data['category_sum']])->select();
        $this->assign('data',$data);
        $this->assign('category_sum',$category_sum);
        $this->assign('category_child',$category_child);
        $this->assign('shieldrule',$shieldrule);
        return $this->fetch('replyfans/editaccountpool');
    }
    public function doEditPool()
    {
        $data = $this->request->param();
        $udata = [
            'poolname'       => $data['poolname'],
            'category_sum'   => $data['category_sum'],
            'category_child' => $data['category_child'],
            'remark'         => $data['remark'],
            'outerlink'      => $data['outerlink'],
            'is_weight'      => $data['is_weight'],
            'shieldrule'     => $data['shieldrule'],
            'rotarygroups'   => $data['rotarygroups'],
            'imgtext_num'    => $data['imgtext_num'],
            'updatetime'     => date('Y-m-d H:i:s',time())
        ];
        $res = Replyfanspool::where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('编辑成功','',$res);
        }else{
            return $this->api_resp_error('编辑失败');
        }
    }
    //复粉账号管理
    public function  replyFansAccount()
    {
        return $this->fetch('replyfans/replyfansaccount');
    }
    //添加复粉账号
    public function addReplyfansAccount()
    {
        $category_sum = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        $this->assign('category_sum',$category_sum);
        return $this->fetch('replyfans/addReplyfansAccount');
    }
    //添加账号
    public function doAddAccount()
    {
        $data = $this->request->param();
        $name = Authorizer::where('ghid',$data['ghid'])->value('name');
        if($name == ''){
            return $this->api_resp_error('原始ID不存在');
        }
        $udata = [
            'gh_id'          => $data['ghid'],
            'name'           => $name,
            'category_sum'   => $data['category_sum'],
            'category_child' => $data['category_child'],
            'accountpool'    => $data['accountpool'],
            'link_img_num'   => $data['link_img_num'],
            'limit_min'      => $data['min'],
            'limit_hour'     => $data['hour'],
            'limit_day'      => $data['day'],
            'createtime'     => date('Y-m-d H:i:s',time()),
        ];
        $res = Replyfansaccount::insert($udata);
        if($res){
            return $this->api_resp_success('添加成功','',$res);
        }else{
            return $this->api_resp_error('添加失败');
        }
    }
    //获取账号列表
    public function getAccountList()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Replyfansaccount::where('gh_id|category_sum|category_child|accountpool','like','%'.$key.'%')
            ->where('delstatus','1')
            ->page($page,$size)
            ->select();
        foreach ($list as $k=>$v){
            $sname = Replyfanscategory::where('id',$v['category_sum'])->value('categoryname');
            $cname = Replyfanscategory::where('id',$v['category_child'])->value('categoryname');
            if($sname == ''){
                $list[$k]['sumname'] = '';
            }else{
                $list[$k]['sumname'] = $sname;
            }
            if($cname == ''){
                $list[$k]['childname'] = '';
            }else{
                $list[$k]['childname'] = $cname;
            }
            $poolname = Replyfanspool::where('id',$v['accountpool'])->value('poolname');
            $list[$k]['poolname'] = $poolname;
        }
        $pagecount = Replyfansaccount::where('gh_id|category_sum|category_child|accountpool','like','%'.$key.'%')
            ->where('delstatus','1')
            ->count();
        return $this->api_resp_success('',array("count"=>$pagecount),$list);
    }
    public function delAccount()
    {
        $aid = $this->request->post('aid');
        $res = Replyfansaccount::where('id','in',$aid)
            ->delete();
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //获取子分类下的账号池
    public function getShieldRule()
    {
        $id = $this->request->post('cid');
        $list = Replyfanspool::where(['delstatus'=>1,'category_child'=>$id])->select();
        return $this->api_resp_success('','',$list);
    }
    //编辑复粉账号
    public function editaccount()
    {
        $aid = $this->request->param('aid');
        $data = Replyfansaccount::where('id',$aid)->find();
        $category_sum = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        if($data['category_child'] !=0){
            $category_child = Replyfanscategory::where(['delstatus'=>1,'level'=>2,'pid'=>$data['category_sum']])->select();
        }else{
            $category_child = '';
        }
        $pool = Replyfanspool::where(['category_child'=>$data['category_child'],'delstatus'=>1])->select();
        $this->assign('data',$data);
        $this->assign('pool',$pool);
        $this->assign('category_sum',$category_sum);
        $this->assign('category_child',$category_child);
        return $this->fetch('replyfans/editaccount');
    }
    public function getCategoryChild()
    {
        $id = $this->request->post('cid');
        $category_child = Replyfanscategory::where(['delstatus'=>1,'pid'=>$id])->select();
        return $this->api_resp_success('','',$category_child);
    }
    public function getCategorySum()
    {
        $category_child = Replyfanscategory::where(['delstatus'=>1,'level'=>1])->select();
        return $this->api_resp_success('','',$category_child);
    }
    public function doEditAccount()
    {
        $data = $this->request->param();
        $udata = [
            'category_sum'=>$data['category_sum'],
            'category_child'=>$data['category_child'],
            'accountpool'=>$data['accountpool'],
            'link_img_num'=>$data['link_img_num'],
            'limit_min'=>$data['limit_min'],
            'limit_hour'=>$data['limit_hour'],
            'limit_day'=>$data['limit_day'],
            'updatetime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Replyfansaccount::where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('修改成功','',$res);
        }else{
            return $this->api_resp_error('修改失败');
        }
    }
    //域名管理
    public function domainManage()
    {
        return $this->fetch('replyfans/domainManage');
    }
    //添加域名
    public function addDomain()
    {
        return $this->fetch('replyfans/addDomain');
    }
    public function doAddDomain()
    {
        $data = $this->request->param();
        $ip = gethostbyname($data['domain']);
        $data = [
            'domain'=>$data['domain'],
            'distribution_num'=>$data['distribution_num'],
            'click_min'=>$data['click_min'],
            'click_hour'=>$data['click_hour'],
            'click_day'=>$data['click_day'],
            'is_weight'=>$data['is_weight'],
            'ip'=>$ip,
            'status'=>1,      //暂时写死  需要进行判断的时候重写逻辑
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_bind_domain')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    //修改域名信息
    public function editDomain()
    {
        $id = $this->request->param('id');
        $data = Db::name('nx_bind_domain')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('replyfans/editDomain');
    }
    public function doEditDomain()
    {
        $data = $this->request->param();
        $udata = [
            'click_min'        =>$data['click_min'],
            'click_hour'       =>$data['click_hour'],
            'click_day'        =>$data['click_day'],
            'distribution_num' =>$data['distribution_num'],
            'is_weight'        =>$data['is_weight'],
            'updatetime'       =>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_bind_domain')->where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function getDomainList()
    {
        $key = $this->request->post('keyword');
        $list = Db::table('nx_bind_domain')
            ->where('domain','like','%'.$key.'%')
            ->where('delstatus',1)->select();
        foreach ($list as $k=>$v){
            switch ($v['status'])
            {
                case 1:
                    $list[$k]['statusname']='正常-未使用';
                        break;
                case 2:
                    $list[$k]['statusname']='正常-使用中';
                    break;
                case 3:
                    $list[$k]['statusname']='校验中';
                    break;
                case 4:
                    $list[$k]['statusname']='未解析';
                    break;
                case 5:
                    $list[$k]['statusname']='解析失败';
                    break;
                case 6:
                    $list[$k]['statusname']='封禁';
                    break;
                case 7:
                    $list[$k]['statusname']='达到次数上限';
                    break;
                case 8:
                    $list[$k]['statusname']='手工下架';
                    break;
                case 9:
                    $list[$k]['statusname']='解析地址不匹配';
                    break;
                default:
                    $list[$k]['statusname']='未知状态';
            }

        }
        return $this->api_resp_success('','',$list);
    }
    public function deleteDomain()
    {
        $id = $this->request->post('id');
        $res = Db::name('nx_bind_domain')->where('id','in',$id)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //监控室
    public function monitor()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('category',$catregory);
        return $this->fetch('replyfans/monitor');
    }
    //复粉总分类
    public function category_sum()
    {
        return $this->fetch('replyfans/category_sum');
    }
    /*
     * 获取复粉总分类
     * */
    public function getFansCategory()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Replyfanscategory::where(['delstatus'=>1,'level'=>1])
            ->where('categoryname','like','%'.$key.'%')
            ->page($page,$size)
            ->order('createtime desc')
            ->select();
        $pagecount = Replyfanscategory::where(['delstatus'=>1,'level'=>1])
            ->where('categoryname','like','%'.$key.'%')
            ->count();
        return $this->api_resp_success('',array("count"=>$pagecount),$list);
    }
    /*
     * 获取复粉子分类
     * */
    public function getFansCategorychild()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Replyfanscategory::where(['delstatus'=>1,'level'=>2])
            ->where('categoryname','like','%'.$key.'%')
            ->page($page,$size)
            ->order('createtime desc')
            ->select();
        $pagecount = Replyfanscategory::where(['delstatus'=>1,'level'=>2])
            ->where('categoryname','like','%'.$key.'%')
            ->count();
        foreach ($list as $k=>$v){
            $name = Replyfanscategory::where('id',$v['pid'])->value('categoryname');
            $list[$k]['category_child'] = $name;
        }
        return $this->api_resp_success('',array("count"=>$pagecount),$list);
    }
    public function addCategorysum()
    {
        return $this->fetch('replyfans/addCategorysum');
    }
    //添加总分类
    public function doAddCategorysum()
    {
        $categoryname = $this->request->post('categoryname');
        $level        = $this->request->post('level');
        $remark       = $this->request->post('remark');
        $pid       = $this->request->post('pid');
        $udata = [
            'categoryname' => $categoryname,
            'remark'       => $remark,
            'level'        => $level,
            'pid'          => $pid,
            'createtime'   => date('Y-m-d H:i:s',time()),
        ];
        $res = Replyfanscategory::insert($udata);
        if($res){
            return $this->api_resp_success('保存成功','',$res);
        }else{
            return $this->api_resp_error('保存失败');
        }
    }
    //编辑总分类
    public function editCategorysum()
    {
        $id = $this->request->param('id');
        $data = Replyfanscategory::where(['id'=>$id,'delstatus'=>1])->find();
        $this->assign('data',$data);
        return $this->fetch('replyfans/editCategorysum');
    }
    public function doEditCategorysum()
    {
        $cid = $this->request->post('cid');
        $categoryname = $this->request->post('categoryname');
        $pid = $this->request->post('pid');
        $remark = $this->request->post('remark');
        $udata = [
            'categoryname' => $categoryname,
            'remark'       => $remark,
            'pid'          => $pid,
            'updatetime'   => date('Y-m-d H:i:s',time()),
        ];
        $res = Replyfanscategory::where('id',$cid)->update($udata);
        if($res){
            return $this->api_resp_success('修改成功','',$res);
        }else{
            return $this->api_resp_error('修改失败');
        }
    }
    public function delCategorysum()
    {
        $ids = $this->request->post('ids');
        $res = Replyfanscategory::where('id','in',$ids)->delete();
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //复粉子分类
    public function category_child()
    {
        return $this->fetch('replyfans/category_child');
    }
    //添加子分类
    public function addCategorychild()
    {
        $list = Db::table('nx_replyfans_category')->where(['delstatus'=>1,'level'=>1])->select();
        $this->assign('list',$list);
        return $this->fetch('replyfans/addCategorychild');
    }
    public function editCategorychild()
    {
        $id = $this->request->param('id');
        $data = Replyfanscategory::where(['id'=>$id,'delstatus'=>1])->find();
        $list = Db::table('nx_replyfans_category')->where(['delstatus'=>1,'level'=>1])->select();
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch('replyfans/editCategorychild');
    }
    public function shieldrule()
    {
        return $this->fetch('replyfans/shieldrule');
    }
    //获取屏蔽规则
    public function getRuleList()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Shieldrule::where('delstatus',1)
            ->where('rulename|city','like','%'.$key.'%')
            ->page($page,$size)->order('createtime desc')->select();
        $pagecount =  Shieldrule::where('delstatus',1)
            ->where('rulename|city','like','%'.$key.'%')->count();
        return $this->api_resp_success('',array('count'=>$pagecount),$list);
    }
    //添加屏蔽规则
    public function addShieldrule()
    {
        return $this->fetch('replyfans/addShieldrule');
    }
    public function doAddRule()
    {
        $data = $this->request->param();
        $udata = [
            'rulename'    => $data['rulename'],
            'city'        => $data['city'],
            'savelink'    => $data['savelink'],
            'voidlink'    => $data['voidlink'],
            'createtime'  => date('Y-m-d H:i:s',time())
        ];
        $res = Shieldrule::insert($udata);
        if($res){
            return $this->api_resp_success('添加成功','',$res);
        }else{
            return $this->api_resp_error('添加失败');
        }
    }
    //编辑屏蔽规则
    public function editShieldrule()
    {
        $id = $this->request->param('id');
        $data = Shieldrule::where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('replyfans/editShieldrule');
    }
    public function doEditShieldrule()
    {
        $data = $this->request->param();
        $udata = [
            'rulename'    => $data['rulename'],
            'city'        => $data['city'],
            'savelink'    => $data['savelink'],
            'voidlink'    => $data['voidlink'],
            'updatetime'  => date('Y-m-d H:i:s',time())
        ];
        $res = Shieldrule::where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('编辑成功','',$res);
        }else{
            return $this->api_resp_error('编辑失败');
        }
    }
    //删除屏蔽规则
    public function delSheildRule()
    {
        $id = $this->request->post('id');
        $res = Shieldrule::where('id','in',$id)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //复粉循环组管理
    public function cyclicgroup()
    {
        return $this->fetch('replyfans/cyclicgroup');
    }
    //获取循环组管理列表
    public function getcyClicgroupList()
    {
        $key  = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Db::table('nx_replyfans_cyclic')->where('delstatus',1)
            ->where('groupname','like','%'.$key.'%')
            ->page($page,$size)
            ->order('createtime desc')
            ->select();
        foreach ($list as $k=>$v){
            $poollist = explode(',',$v['tier']);
            $pools = '';
            foreach ($poollist as $ke=>$val){
                $pool = Replyfanspool::where('id',$val)->value('poolname');
                $pools = $pools.'->'.$pool;
                $pools = trim($pools,'->');
            }
            $list[$k]['tiername'] = $pools;
        }
        $pagecount =  Db::table('nx_replyfans_cyclic')->where('delstatus',1)
            ->where('groupname','like','%'.$key.'%')->count();
        return $this->api_resp_success('',array('count'=>$pagecount),$list);
    }
    //添加循环组
    public function addCyclicGroup()
    {
        $list = Replyfanspool::where('delstatus',1)->select();
        $this->assign('list',$list);
        return $this->fetch('replyfans/addCyclicGroup');
    }
    public function doAddCyclic()
    {
        $data = $this->request->param();
        $tier = implode(',',$data['vallist']);
        $udata = [
            'groupname'  => $data['groupname'],
            'remark'     => $data['remark'],
            'tier'    => $tier,
            'createtime' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::table('nx_replyfans_cyclic')->insert($udata);
        if($res){
            return $this->api_resp_success('添加成功','',$res);
        }else{
            return $this->api_resp_error('添加失败');
        }
    }
    //上架下架
    public function putAway()
    {
        $data = $this->request->param();
        $udata = [
            'status'     => $data['param'],
            'updatetime' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::table('nx_replyfans_cyclic')->where('id',$data['id'])->update($udata);
        if($res){
            return $this->api_resp_success('操作成功','',$res);
        }else{
            return $this->api_resp_error('操作失败');
        }
    }
    //复粉图文库
    public function imagetext()
    {
        return $this->fetch('replyfans/imagetext');
    }
    //获取图文库列表
    public function getImgTextList()
    {
        $key  = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Replyfansimgtext::where('delstatus',1)->where('title','like','%'.$key.'%')
            ->page($page,$size)->order('createtime desc')->select();
        $pagecount = Replyfansimgtext::where('delstatus',1)->where('title','like','%'.$key.'%')->count();
        foreach ($list as $k => $v){
            $sumname = Replyfanscategory::where('id',$v['category_sum'])->value('categoryname');
            $childname = Replyfanscategory::where('id',$v['category_child'])->value('categoryname');
            $poolname = Replyfanspool::where('id',$v['accountpool'])->value('poolname');
            $list[$k]['sumname'] = $sumname;
            $list[$k]['childname'] = $childname;
            $list[$k]['poolname'] = $poolname;
        }
        return $this->api_resp_success('',array('count'=>$pagecount),$list);
    }
    //添加图文
    public function addImgText()
    {
        return $this->fetch('replyfans/addImgText');
    }
    //图文库上传图片
    public function uploadimgtext()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = $this->request->file('fileList');
        // 移动到框架应用根目录/uploads/ 目录下
        $rootpath = Env::get('root_path');
        $info = $file->move($rootpath .'public/static/uploads');
        if($info){
            // 成功上传后 获取上传信息
            $files = 'http://wechat.nanxuncn.cn/static/uploads/'.$info->getSaveName();
            return json(array('code' => 0,'msg' => $files));
        }else{
            // 上传失败获取错误信息
            return json(array('code'=>4,'msg'=>$file->getError()));
        }
    }
    public function doAddImgText()
    {
        $data = $this->request->param();
        $udata = [
            'title' => $data['title'],
            'imgurl' => $data['imgurl'],
            'category_sum' => $data['category_sum'],
            'category_child' => $data['category_child'],
            'accountpool' => $data['accountpool'],
            'img_local' => $data['img_local'],
            'priority' => $data['priority'],
            'createtime' => date('Y-m-d H:i:s',time()),
        ];
        $res = Replyfansimgtext::insert($udata);
        if($res){
            return $this->api_resp_success('保存成功','',$res);
        }else{
            return $this->api_resp_error('保存失败');
        }
    }
    public function delImgText()
    {
        $ids = $this->request->post('aid');
        if($ids == ''){
            return $this->api_resp_error('请选择需要删除的图文');
        }
        $res = Replyfansimgtext::where('id','in',$ids)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('删除成功','',$res);
        }else{
            return $this->api_resp_error('删除失败');
        }
    }
    //编辑图文
    public function editImgText()
    {
        $id = $this->request->param('aid');
        $data = Replyfansimgtext::where('id',$id)->find();
        $list = Replyfanscategory::where(['level'=>1,'delstatus'=>1])->select();
        $listchild = Replyfanscategory::where(['pid'=>$data['category_sum'],'delstatus'=>1])->select();
        $pool = Replyfanspool::where(['delstatus'=>1,'category_child'=>$data['category_child']])->select();
        $this->assign('list',$list);
        $this->assign('listchild',$listchild);
        $this->assign('data',$data);
        $this->assign('pool',$pool);
        return $this->fetch('replyfans/editImgText');
    }
    public function doEditImgText()
    {
        $data = $this->request->param();
        //原图片路径
        $url = Replyfansimgtext::where('id',$data['id'])->value('imgurl');
        $udata = [
            'title'          => $data['title'],
            'category_sum'   => $data['sum'],
            'category_child' => $data['child'],
            'accountpool'    => $data['pool'],
            'priority'       => $data['priority'],
            'img_local'      => $data['img_local'],
            'imgurl'         => $data['img'],
            'updatetime'     => date('Y-m-d H:i:s',time()),
        ];
        $res = Replyfansimgtext::where('id',$data['id'])->update($udata);
        if($res){
            $root = Env::get('root_path');
            $rootpath = $root.'public';
            $oldpath = str_replace('http://wechat.nanxuncn.cn',$rootpath,$url);
            unlink($oldpath);
            return $this->api_resp_success('编辑成功','',$res);
        }else{
            return $this->api_resp_error('编辑失败');
        }
    }
}
